package view.face
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import global.GameState;
	
	import utils.ColorTool;
	
	public class Hint extends Sprite
	{
		protected var hintText:TextField;
		protected var currentTarget:DisplayObject;
		public static var tempText:TextField;
		protected var _isUp:Boolean;
		protected var miniWidth:int = 20;
		
		public function Hint()
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			ColorTool.dropShadow(this);
			initHintText();
		}
		
		private function initHintText():void
		{
			hintText = new TextField();
			hintText.textColor = 0xFFFFFF;
			hintText.multiline = true;
			hintText.wordWrap = true;
			hintText.selectable = false;
			
			var tf:TextFormat = new TextFormat();
			tf.indent = 4;
			hintText.defaultTextFormat = tf;
		
			addChild(hintText);
			
			if (tempText == null)
			{
				tempText = new TextField();
				tempText.width = 1000;
			}
		}
		
		public function onlyUpdateContent(content:String):void
		{
			
		}
		
		public function showHint(target:DisplayObject,content:String,isUp:Boolean = false):void
		{
			_isUp = isUp;
			currentTarget = target;
			var point:Point = target.localToGlobal(new Point());
			point.y = point.y + target.height;
			point.x = point.x + target.width * .5;
			
			hintText.htmlText = content;
			tempText.htmlText = content;
			hintText.width = tempText.textWidth + 9;
			if (hintText.width > 200)
			{
				hintText.width = 200;
			}
			else if (hintText.width < miniWidth)
			{
				hintText.width = miniWidth;
			}
			
			hintText.height = hintText.textHeight + 5;
			drawBg();
			setXY(point.x,point.y);
		}
		
		protected function setXY(x:Number,y:Number):void
		{
			if (_isUp)
			{
				this.y = y - currentTarget.height - this.height; 
			}
			else
			{
				if (this.height + y + 10 > GameState.GAME_HEIGHT)
				{
					this.y = y - currentTarget.height - this.height; 
				}
				else
				{
					this.y = y + 10;
				}
			}
			this.x = x - this.width /2;
			//trace(this.width, this.x);
			if (this.width + this.x > GameState.GAME_WIDTH)
			{
				this.x = GameState.GAME_WIDTH - this.width;
				//trace(this.x);
			}
			else if ( this.x < 0)
			{
				this.x = 0;
			}
			else 
			{
				
			}
			//trace(this.width / 2 + this.x);
		}

		protected function drawBg():void
		{
			with(this.graphics)
			{
				clear();
				//lineStyle(1,0xFFFFFF,.6);
				beginFill(0xFFFFFF,1);
				drawRoundRect(-2,-2,hintText.width + 4,hintText.height + 4,12,12);
				//trace("drawWH",hintText.width,hintText.textHeight);
				endFill();
			}
		}
	}
}